package sitemaps;

import java.text.SimpleDateFormat;
import java.util.TimeZone;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class SitemapBuilder {

	private final SimpleDateFormat formatISO8601;
	private DocumentBuilder documentBuilder;
	private String changefreq;
	private String priority;
	
	public SitemapBuilder(String dateFormat, String zone, String changefreq, String priority) throws ParserConfigurationException {
		final TimeZone timeZone = TimeZone.getTimeZone(zone);
		formatISO8601 = new SimpleDateFormat(dateFormat);
		formatISO8601.setTimeZone(timeZone);

		final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		documentBuilder = factory.newDocumentBuilder();
		this.changefreq = changefreq;
		this.priority = priority;
	}
	
	public Document build(Sitemap site) {
		
		final Document document = documentBuilder.newDocument();

		final Element urlset = document.createElementNS(SITEMAP_NS, URLSET);
		final Attr attr = document.createAttributeNS(SCHEMA_NS, SCHEMA_LOCATION);
		attr.setPrefix(XSI);
		attr.setTextContent(SITEMAP_SCHEMA);
		urlset.setAttributeNodeNS(attr);
		document.appendChild(urlset);
		
		for(SitemapUrl siteUrl: site.values()) {

			final Element url = document.createElement(URL);
			urlset.appendChild(url);

			final Element loc = document.createElement(LOC);
			loc.setTextContent(siteUrl.getLoc());
			url.appendChild(loc);
			
			final Element lastmod = document.createElement(LASTMOD);
			lastmod.setTextContent(formatISO8601.format(siteUrl.getDate()));
			url.appendChild(lastmod);
			
			final Element changefreq = document.createElement(CHANGEFREQ);
			changefreq.setTextContent(this.changefreq);
			url.appendChild(changefreq);
			
			final Element priority = document.createElement(PRIORITY);
			priority.setTextContent(this.priority);
			url.appendChild(priority);
			
		}
		
		return document;
	}

	private static final String XSI = "xsi";
	private static final String URL = "url";
	private static final String LOC = "loc";
	private static final String URLSET = "urlset";
	private static final String LASTMOD = "lastmod";
	private static final String PRIORITY = "priority";
	private static final String CHANGEFREQ = "changefreq";
	private static final String SCHEMA_LOCATION = "schemaLocation";
	private static final String SCHEMA_NS = "http://www.w3.org/2001/XMLSchema-instance";
	//private static final String SITEMAP_NS = "http://www.google.com/schemas/sitemap/0.84";
	private static final String SITEMAP_NS = "http://www.sitemaps.org/schemas/sitemap/0.9";
	//private static final String SITEMAP_SCHEMA = SITEMAP_NS + " " + "http://www.google.com/schemas/sitemap/0.84/sitemap.xsd";
	private static final String SITEMAP_SCHEMA = SITEMAP_NS + " " + "http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd";

}
